import numpy as np

# ---- run Test
num: int = 0


def runTest(fun):
    global num
    print('----------------------', num)
    fun()
    num += 1
    print()


# -------------------------------------------- tests:

def test_flat():
    a = np.arange(9).reshape(3, 3)
    for row in a:
        print(row)
    print()
    # 使用flat属性：
    for ele in a.flat:
        print(ele, end="，")


def test_flatten():
    a = np.arange(8).reshape(2, 4)
    print(a)
    # 默认按行C风格展开的数组
    print(a.flatten())
    # 以F风格顺序展开的数组
    print(a.flatten(order='F'))


def test_ravel():
    a = np.arange(8).reshape(2, 4)
    print('原数组：')
    print(a)
    print('调用 ravel 函数后：')
    print(a.ravel())
    print('F 风格顺序调用 ravel 函数之后：')
    print(a.ravel(order='F'))


def test_transpose():
    a = np.arange(12).reshape(3, 4)
    print(a)
    print(np.transpose(a))


# rollaxis 新版 moveaxis
def test_moveaxis():
    a = np.arange(3 * 4 * 5).reshape(3, 4, 5)
    print(a.shape)
    print(np.moveaxis(a, 2, 0).shape)


def test_swapaxis():
    # 创建了三维的 ndarray
    a = np.arange(3 * 4 * 5).reshape(3, 4, 5)
    print(a.shape)
    # 对换0轴与2轴
    print(np.swapaxes(a, 2, 0).shape)


def test_broadcast():
    a = np.array([[1], [2], [3]])
    b = np.array([4, 5, 6])
    # 对b广播a
    d = np.broadcast(a, b)
    # d拥有 iterator 属性
    print('d1 iters:\n')
    [print(x1, y1) for (x1, y1) in d]

    d2 = np.broadcast(b, a)
    # d拥有 iterator 属性
    print('d2 iters:\n')
    [print(x1, y1) for (x1, y1) in d2]

    print()
    # 使用broadcast将a与b相加
    e = np.broadcast(a, b)
    f = np.empty(e.shape)
    f.flat = [x + y for (x, y) in e]
    print(f)
    print(a + b)


def test_broadcast_to():
    a = np.arange(4).reshape(1, 4)
    print("原数组", a)
    print('调用 broadcast_to 函数之后：')
    print(np.broadcast_to(a, (4, 4)))


def test_expand_dim():
    x = np.array(([1, 2], [3, 4]))
    print('数组 x：\n', x)

    # 在 0 轴处插入新的轴
    y = np.expand_dims(x, axis=0)
    print('数组 y：\n', y)

    print('数组 x 和 y 的形状：')
    print(x.shape, y.shape)


def test_squeeze():
    a = np.arange(9).reshape(1, 3, 3)
    print('#a_\n', a)
    b = np.squeeze(a)
    print('#b\n', b)
    print('数组 a_ 和 b 的形状：')
    print(a.shape, b.shape)


def test_concatenate():
    # 创建数组a
    a = np.array([[10, 20], [30, 40]])
    print('#a_\n', a)
    # 创建数组b
    b = np.array([[50, 60], [70, 80]])
    print('#b\n', b)
    print('# 沿轴 0 连接两个数组')
    print(np.concatenate((a, b)))
    print('# 沿轴 1 连接两个数组')
    print(np.concatenate((a, b), axis=1))


def test_vstack():
    a = np.array([[1, 2], [3, 4]])
    b = np.array([[5, 6], [7, 8]])
    print('# 垂直堆叠')
    c = np.vstack((a, b))
    print(c)


def test_split():
    a = np.arange(12)
    print('# 原数组')
    print(a)
    print('# 将数组分为二个形状大小相等的子数组')
    b = np.split(a, 2)
    print(b)
    print('# 将数组在一维数组中标明要位置分割')
    b = np.split(a, [3, 5, 8])
    print(b)


def test_hsplit():
    print('# arr1数组')
    arr1 = np.floor(10 * np.random.random((2, 6)))
    print(arr1)
    print('# 拆分后数组')
    print(np.hsplit(arr1, 3))


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    runTest(test_flat)
    runTest(test_flatten)
    runTest(test_ravel)
    runTest(test_transpose)
    runTest(test_moveaxis)
    runTest(test_swapaxis)
    runTest(test_broadcast)
    runTest(test_broadcast_to)
    runTest(test_expand_dim)
    runTest(test_squeeze)
    runTest(test_concatenate)
    runTest(test_vstack)
    runTest(test_split)
    runTest(test_hsplit)
